package com.zbw.weightlibrary.Util;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 可缓存线程池
 */
public class ThreadPool {
    private static ExecutorService pool;

    private ThreadPool(){}

    private static void init(){
        if(pool == null) {
            synchronized (ThreadPool.class) {
                if(pool == null) {
                    pool = Executors.newCachedThreadPool();
                }
            }
        }
    }

    public static void execute(Runnable runnable){
        init();
        pool.execute(runnable);
    }

    public static void close(){
        //池子不为空且没有关闭
        if(pool != null && !pool.isShutdown()){
            //停止正在执行的任务和等待的任务
            pool.shutdownNow();
            pool = null;
        }
    }
}
